﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace DBSourceToolsLib
{
    public class SourceDatabaseWriter : Interfaces.IProgressEvent, Interfaces.ISourceDatabaseCommand
    {
        bool m_bIsZipFile = false;
        SourceDatabase m_SourceDatabase;
        Form m_ParentForm;
        Commands.DatabaseCommandTypes m_CommandType;
        DatabaseProject m_DatabaseProject;

        bool m_HasErrors = false;
        string m_ErrorMessage = null;

        public string ErrorMessage
        {
            get { return m_ErrorMessage; }
            set
            {
                if (!String.IsNullOrEmpty(value))
                {
                    m_HasErrors = true;
                    m_ErrorMessage = value;
                }
            }
        }

        public SourceDatabaseWriter(Form oForm
            , Commands.DatabaseCommandTypes oCommandType
            , DatabaseProject oProject)
        {
            m_ParentForm = oForm;
            m_CommandType = oCommandType;
            m_DatabaseProject = oProject;
        }


        #region IProgressEvent Members

        public event EventHandler<ProgressEventArgs> ProgressEventPublisher;

        public void Execute()
        {
                EventHandler<ProgressEventArgs> localHandler = ProgressEventPublisher;

                switch (m_CommandType)
                {
                    case DBSourceToolsLib.Commands.DatabaseCommandTypes.WriteToDisk:
                        m_SourceDatabase.SysObjects.WriteFiles(m_SourceDatabase.ScriptOptions, localHandler);
                        m_SourceDatabase.SysObjects.WriteDependencyTree();
                        if (m_SourceDatabase.SysObjects.HasErrors())
                        {
                            this.ErrorMessage = m_SourceDatabase.SysObjects.GetErrorMessage();
                        }
                        else
                        {
                            m_SourceDatabase.ScriptOptions.WriteToDisk();

                            foreach (TargetDatabase oTarget in m_SourceDatabase.TargetDatabases)
                            {
                                oTarget.WriteCreateScript(this.m_DatabaseProject, localHandler, m_bIsZipFile);
                                if (!String.IsNullOrEmpty(oTarget.ErrorMessage))
                                {
                                    this.ErrorMessage = oTarget.ErrorMessage;
                                    break;
                                }

                            }
                        }

                        break;

                }

                if (localHandler != null)
                    localHandler(this, new ProgressEventArgs(100, 100,
                        "Processing Complete"));

        }

        #endregion

        #region ISourceDatabaseCommand Members

        public void Execute(ref SourceDatabase oRefSourceDatabase, string strParams)
        {
            if (!String.IsNullOrEmpty(strParams))
                if (strParams.CompareTo("isZip") == 0)
                    m_bIsZipFile = true;

            m_SourceDatabase = oRefSourceDatabase;

            if (m_ParentForm != null)
            {
                UI.Forms.ProgressForm oProgressForm = new DBSourceToolsLib.UI.Forms.ProgressForm();
                oProgressForm.setEventSource(this);
                oProgressForm.ShowDialog(m_ParentForm);
            }
            else
            {

                

                //Execute();
                // manually run from the command line

            }

        }

        #endregion

        #region IProgressEvent Members

        public void RegisterError(string _ErrorMessage)
        {
            m_HasErrors = true;
            m_ErrorMessage = _ErrorMessage;
        }

        #endregion

        #region IProgressEvent Members


        public bool HasErrors()
        {
            return m_HasErrors;
        }

        public string GetErrorMessage()
        {
            return m_ErrorMessage;
        }

        #endregion
    }
}
